package org.jgs2007.onemall.controller;

import com.github.pagehelper.PageInfo;
import org.jgs2007.onemall.entity.User;
import org.jgs2007.onemall.service.UserService;
import org.jgs2007.onemall.util.RedisUtils;
import org.jgs2007.onemall.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtils redisUtils;

    @RequestMapping("list")
    public Object show(@RequestParam(name = "page", required = false, defaultValue = "1") Integer pageNum, @RequestParam(name = "limit", required = false, defaultValue = "10") Integer pageSize){

        PageInfo<User> users = userService.getAll(pageNum,pageSize);

        if(users.getList().isEmpty()){

            throw new RuntimeException("暂无数据！");

        }

        Map<Object, Object> data = new HashMap<>();
        data.put("limit", pageSize);
        data.put("list", users.getList());
        data.put("page", users.getPageNum());
        data.put("pages", users.getPages());
        data.put("total", users.getTotal());

        return ResponseUtil.ok(data);
    }
}
